package org.ezplatform.office.meeting.setting.base.web.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ezplatform.cloud.common.log.LogService;
import org.ezplatform.core.annotation.MetaData;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.service.BaseService;
import org.ezplatform.core.web.controller.BaseController;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.mobile.cocall.common.service.CocallMessageService;
import org.ezplatform.mobile.util.common.service.CommonService;
import org.ezplatform.office.meeting.common.util.MeetingUtils;
import org.ezplatform.office.meeting.setting.base.entity.BaseSet;
import org.ezplatform.office.meeting.setting.base.service.BaseSetService;
import org.ezplatform.office.meeting.setting.boardroom.entity.BoardroomSet;
import org.ezplatform.office.meeting.setting.boardroom.service.BoardroomSetService;
import org.ezplatform.office.meeting.setting.type.service.MeetingTypeService;
import org.ezplatform.util.DateUtils;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: OaMeetingBaseSetController
 * @Description: 会议基础设置控制类
 * @author: pa
 * @date 2018-03-02 10:29:33
 */
@MetaData("会议基础设置")
@Controller
@RequestMapping(value = "/baseSetController")
public class BaseSetController extends BaseController<BaseSet, String> {
	private static final Logger LOGGER = LoggerFactory.getLogger(BaseSetController.class);
	@Autowired
	private BaseSetService service;

	@Autowired
    private BoardroomSetService boardroomSetService;

	@Autowired
    private MeetingTypeService meetingTypeService;
	
	@Autowired
	private MeetingUtils meetingUtils;
	
	@Autowired
	private LogService logService;
	
	@Autowired
	private CommonService commonService;
	
	@Override
	protected BaseService<BaseSet, String> getEntityService() {
		return service;
	}

	/**
	 * 
	 * @Title: prepareModel
	 * @Description: 初始化数据
	 * @param request
	 * @param model
	 * @param id
	 * @return void
	 * @throws
	 */
	@ModelAttribute
	public void prepareModel(HttpServletRequest request, Model model,
			@RequestParam(value = "id", required = false) String id) {
		super.initPrepareModel(request, model, id);
	}

	 /**
     * 保存/修改
     *
     * @param entity
     * @param model
     * @return
     */
    @RequestMapping(value = "/editSave", method = RequestMethod.POST)
    @ResponseBody
    public OperationResult editSave(@ModelAttribute("entity") BaseSet entity, Model model) {
    	WebUser user = WebUtils.getCurrentUser();
    	String title = "同步日程";
    	if(entity.getIsCalendar() == 1) title += "开启";
    	else if(entity.getIsCalendar() == 0) title += "关闭";
    	if(logService.checkOptLog(user.getCorpId(), "meetingLog:setting:02", user.getJwtToken()))//判断是否记录日志
	    	this.logService.saveModuleLog("meetingLog:setting:02", title, ""); //记录日志 
        return super.editSave(entity);
    }
    
    /**
     * 编辑页面
     * 
     * @param model
     * @return
     */
    @RequestMapping(value = "/editShow", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> editShow(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        WebUser user = WebUtils.getCurrentUser();
        String corpId = user.getCorpId();
        BaseSet baseSet = this.service.getBaseSetInfo(corpId);
        result.put("entity", baseSet);
        return result;
    }
    
    /**
     * 
     */
    @RequestMapping(value = "/getBaseSetInfo", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getBaseSetInfo(HttpServletRequest request) {
    	
    	LOGGER.debug("-------------getBaseSetInfo---------------");
    	
        Map<String, Object> result = new HashMap<>();
        
        WebUser user = WebUtils.getCurrentUser();
        String corpId = user.getCorpId();
        // 查询基础设置
//        String noticeType = "";
//        BaseSet baseSet = this.service.getBaseSetInfo(corpId);
//        if (baseSet != null) {
//            noticeType = baseSet.getNoticeType();
//        }
        String noticeType = commonService.getSysParamValueByMark(corpId, TOKEN,"sysImType");
        result.put("noticeType", noticeType);
        String messagePushSW = commonService.getMessagePush(corpId, TOKEN,"meeting");
        result.put("messagePushSW", messagePushSW);
        //当前日期
        String hidApplyRecordDate = DateUtils.formatDate(DateUtils.now());
        result.put("hidApplyRecordDate", hidApplyRecordDate);
        SimpleDateFormat sdf1 = new SimpleDateFormat("HH");
		String nowHH = sdf1.format(new Date());
		result.put("hidApplyRecordHH", nowHH);
		SimpleDateFormat sdf2 = new SimpleDateFormat("mm");
		String nowmm = sdf2.format(new Date());
		if(Integer.parseInt(nowmm)>30) {
			nowmm = "00";
		}else {
			nowmm = "30";
		}
		result.put("hidApplyRecordmm", nowmm);
		
        //当前用户
        String userId = user.getUserId();
        result.put("emceeUser", "[{'scopeType':'user','scopeValue':'"+userId+"','scopeName':'"+user.getCnName()+"'}]");
        result.put("emceeName", user.getCnName());//默认主持人

        //会议室
        String roomId = StringUtils.null2String(request.getParameter("roomId"));
        String roomName = "";
        String limitDate = "";
        if(!"".equals(roomId)){
            BoardroomSet room = this.boardroomSetService.findOne(roomId);
            if(room != null){
                roomName = room.getName();
                limitDate = this.boardroomSetService.getLimitDate(room.getBeforeDays());
            }
        }
        result.put("boardRoomName", roomName);
        result.put("limitDate", limitDate);
        result.put("meetingTypes", meetingTypeService.getMeetingTypes(corpId));
        
        return result;
    }
}
